package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.mapper.UserMapper;
import cn.tedu.coolshark.pojo.dto.UserDto;
import cn.tedu.coolshark.pojo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author liyang
 * @version 1.0
 * @package cn.tedu.coolshark.controller
 * @file UserController.java
 * @description
 * @time 2024/8/6 下午8:10
 */
@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/login")
    public int login(@RequestBody UserDto user, HttpSession session){
        System.out.println("user = " + user );
        UserVO u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            if(user.getPassword().equals(u.getPassword())){
                session.setAttribute("user", u);


                return 1;//正确➡前端直接跳转到首页
            }
            return 3;//密码错误➡前端仅进行$提示
        }
        return 2;//不存在
    }

    @RequestMapping("/currenUser")
    public UserVO currenUser(HttpSession session){
        return (UserVO)session.getAttribute("user");
    }

    @RequestMapping("/logout")
    public void logout(HttpSession session){
        session.removeAttribute("user");
    }



}
